<?php
session_start();

include_once( './data/defines.php' );
include_once( './api/api_user.php' );

$ret = array(
    FLAG_RET => ERROR,
    FLAG_ERR_CODE => ERR_UNKNOWN
);

switch($_REQUEST['action']) {

    //登录
    case 'login': {
        $access_token = $_REQUEST['access_token'];
        $weibo_userid = $_REQUEST['weibo_userid'];
        if(isset($access_token) && isset($weibo_userid) && $access_token != "" && $weibo_userid != "") {
            $ret = user_login($weibo_userid);
            if($ret[FLAG_RET] == NO_ERR) {
                //记录session信息，返回session_id
                $_SESSION[SESS_ACCESS_TOKEN] = $access_token;
                $_SESSION[SESS_WEIBO_USER_ID] = $weibo_userid;
                $_SESSION[SESS_USER_ID] = $ret[FLAG_USER_ID];
                $ret = array(
                    FLAG_RET => NO_ERR,
                    FLAG_SESSION_ID => session_id()
                );
            } else {
                $ret = array(
                    FLAG_RET => ERROR,
                    FLAG_ERR_CODE => ERR_USER_NOT_FOUND
                );
            }
        } else {
            $ret = array(
                FLAG_RET => ERROR,
                FLAG_ERR_CODE => ERR_VAL,
                "access_token" => $access_token,
                "weibo_userid" => $weibo_userid
            );
        }
        break;
    }
    
    default: {
        $ret = array(
            FLAG_RET => ERROR,
            FLAG_ERR_CODE => ERR_VAL
        );
        break;
    }
}

echo json_encode($ret);
?>